﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class NavDao
    {


        public NavDao()
        {

            Mapper.CreateMap<E_Nav, NavModel>();
        }
        public List<E_Nav> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_Nav.Count();

                return edm.E_Nav.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public NavModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Nav.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_Nav, NavModel>(query);
            }

        }


        public int Save(NavModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Nav.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_Nav();

                    edm.AddToE_Nav(query);

                }


                query.NavName = obj.NavName;

                query.NavEName = obj.NavEName;

                query.NavDesc = obj.NavDesc;
                query.NavEDesc = obj.NavEDesc;

                query.IsShow = obj.IsShow;

                query.ParentID = obj.ParentID;

                query.CreateTime = obj.CreateTime;

                query.SeoTitile = obj.SeoTitile;

                query.SeoEtitle = obj.SeoEtitle;

                query.SeoKey = obj.SeoKey;

                query.SeoEkey = obj.SeoEkey;

                query.SeoDesc = obj.SeoDesc;

                query.SeoEDesc = obj.SeoEDesc;

                query.OrderId = obj.OrderId;

                query.CreateUserId = obj.CreateUserId;

                query.ContentETemplate = obj.ContentETemplate;

                query.ContentTemplate = obj.ContentTemplate;
                query.SiteID = obj.SiteID;
                query.IsSingle = obj.IsSingle;

                edm.SaveChanges();

                return query.Id;


            }

        }
        public List<NavModel> GetListBySiteid(int siteid)
        {
             using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                return Mapper.Map<List<E_Nav>, List<NavModel>>(edm.E_Nav.Where(s => s.SiteID == siteid).OrderBy(s=>s.OrderId).ToList());
             }
        }

        public List<NavModel> GetListByPid(int pid)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                return Mapper.Map<List<E_Nav>, List<NavModel>>(edm.E_Nav.Where(s=>s.ParentID==pid).ToList());
            }
        }

        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Nav.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

